package city.spring.modules.reception.service.impl;

import city.spring.modules.oldman.entity.OldmanInfoEntity;
import city.spring.modules.oldman.service.OldmanInfoService;
import city.spring.modules.reception.entity.AdvisoryEntity;
import city.spring.modules.reception.entity.ReservationEntity;
import city.spring.modules.reception.repository.AdvisoryRepository;
import city.spring.modules.reception.service.AdvisoryService;
import city.spring.modules.reception.service.ReservationService;
import city.spring.modules.reception.vo.AdvisoryInfoVO;
import city.spring.modules.reception.vo.AdvisoryListVO;
import city.spring.modules.system.service.UserService;
import city.spring.utils.UserUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：咨询记录
 *
 * @author 侯坤林
 * @date 2020-06-23 10:56:54
 */
@CacheConfig(cacheNames = {AdvisoryServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class AdvisoryServiceImpl extends ServiceImpl<AdvisoryRepository, AdvisoryEntity> implements AdvisoryService {
    private final OldmanInfoService oldmanInfoService;
    private final ReservationService reservationService;
    private final UserService userService;
    private final AdvisoryRepository advisoryRepository;

    public AdvisoryServiceImpl(OldmanInfoService oldmanInfoService, ReservationService reservationService, UserService userService, AdvisoryRepository advisoryRepository) {
        this.oldmanInfoService = oldmanInfoService;
        this.reservationService = reservationService;
        this.userService = userService;
        this.advisoryRepository = advisoryRepository;
    }

    @Override
    public void saveAdvisory(AdvisoryInfoVO entity) {
        OldmanInfoEntity oldmanInfo = entity.getOldmanInfo();
        OldmanInfoEntity findOldmanInfo = oldmanInfoService.lambdaQuery()
                .eq(OldmanInfoEntity::getCertificateType, oldmanInfo.getCertificateType())
                .eq(OldmanInfoEntity::getCertificate, oldmanInfo.getCertificate())
                .one();
        if (findOldmanInfo != null) {
            Integer status = findOldmanInfo.getStatus();
            // if (status == 1 || status == 2) {
            //     throw new RuntimeException("老人已经入住");
            // }
            oldmanInfo.setId(findOldmanInfo.getId());
            if (status == 4) {
                // 老人信息状态是咨询状态，则直接保存完整的内容
                oldmanInfoService.updateById(oldmanInfo);
            } else {
                // 重新设置老人的家庭地址信息
                findOldmanInfo.setFamilyAddress(oldmanInfo.getFamilyAddress());
                oldmanInfoService.updateById(findOldmanInfo);
            }
        } else {
            oldmanInfo.setStatus(4);
            oldmanInfoService.saveOldmanInfo(oldmanInfo);
        }

        ReservationEntity reservation = entity.getReservation();

        reservationService.saveReservation(reservation);

        AdvisoryEntity advisory = entity.getAdvisory();
        advisory.setOldmanId(oldmanInfo.getId());
        advisory.setReservationId(reservation.getId());
        // 设置接待用户为当前登录用户
        advisory.setReceptionUserId(UserUtils.getPrincipal().getName());
        advisory.setIsReview(1);
        if (!save(advisory)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateAdvisory(AdvisoryInfoVO entity) {
        // 直接修改老人信息
        OldmanInfoEntity oldmanInfo = entity.getOldmanInfo();
        oldmanInfoService.updateById(oldmanInfo);

        ReservationEntity reservation = entity.getReservation();
        reservationService.updateReservation(reservation);


        // 修改咨询信息
        AdvisoryEntity advisory = entity.getAdvisory();

        if (advisory.getReviewDatetime() != null) {
            advisory.setReviewUserId(UserUtils.getPrincipal().getName());
            advisory.setIsReview(0);
        }

        if (!updateById(advisory)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteAdvisory(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteAdvisory(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public Page<AdvisoryListVO> queryPage(Page<? extends AdvisoryEntity> page, Wrapper<? extends AdvisoryEntity> wrapper) {
        return baseMapper.queryPage(page, wrapper);
    }

}